<?php

namespace app\admin\controller;

use Workerman\Crontab\Crontab;
use Workerman\Lib\Timer;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use think\Controller;
use think\controller\Rest;
use think\Db;
use think\Log;
use think\Request;

class Siren extends Common
{
    /**
     * 预警配置
     * */
    public function setSiren()
    {
        if (Request()->isPost()) {
            $input = Request()->param();
            
            $basics_gap = $input['basics_gap'];
            $max_gap = $input['max_gap'];
            $max_siren_count = $input['max_siren_count'];
            $siren_time = $input['siren_time'];
            $sms_siren = $input['sms_siren'];
            $sms_mobile = $input['sms_mobile'];
            $monitor_gap = $input['monitor_gap'];
            $time_arr = explode(' - ', $siren_time);
            $start_time = $time_arr[0];
            $end_time = $time_arr[1];
            
            if (empty($basics_gap)) adminReturn(2, '请输入报警间隔');
            if ($max_gap < 5) adminReturn(2, '报警间隔至少5分钟');
            if (empty($max_gap)) adminReturn(2, '请输入顶峰报警间隔');
            if ($max_gap < 60) adminReturn(2, '顶峰报警间隔至少60分钟');
            if (empty($max_siren_count)) adminReturn(2, '请输入顶峰报警次数');
            if (empty($start_time) || empty($end_time)) adminReturn(2, '请选择报警时间段');
            if ($start_time >= $end_time) adminReturn(2, '报警时间段设置有误');
            if (empty($sms_siren)) adminReturn(2, '请输入短信报警时间');
            if (empty($sms_mobile)) adminReturn(2, '请输入短信报警手机号');
            if (!isTel($sms_mobile)) adminReturn(2, '请输入正确的手机号');
            if (empty($monitor_gap)) adminReturn(2, '请输入监测间隔');
            
            $data = [
                'basics_gap' => $basics_gap,
                'max_gap' => $max_gap,
                'max_siren_count' => $max_siren_count,
                'start_time' => $start_time,
                'end_time' => $end_time,
                'sms_siren' => $sms_siren,
                'sms_mobile' => $sms_mobile,
                'monitor_gap' => $monitor_gap,
            ];
            
            $count = Db::name('siren_config')->count();
            if ($count == 0) {
                $res = Db::name('siren_config')->insert($data);
            } else {
                $res = Db::name('siren_config')->where('1 = 1')->update($data);
            }
            
            if ($res) {
                $siren_time_gap = getSirenTimeGap($data);
                $data['siren_time_gap'] = $siren_time_gap;
                cache('siren_config', $data);
                
                adminReturn(1, '保存成功');
            } else {
                adminReturn(2, '保存失败或未修改');
            }
        } else {
            $info = Db::name('siren_config')->find();
            $info['siren_time'] = $info['start_time'] && $info['end_time'] ? $info['start_time'] . ' - ' . $info['end_time'] : '';
            $this->assign('info', $info);
        }
        
        $this->assign('title', '预警配置');
        
        return $this->fetch();
    }
    
    /**
     * 预警列表
     * */
    public function sirenList()
    {
        $input = Request()->param();
        
        $date = $input['date'];
        $type = $input['type'];
        
        if ($date) $where['date'] = $date;
        if ($type) $where['type'] = $type;
        
        // 排序
        $order_sql = Db::name('crontab_log')->order('id desc')->buildSql();
        // 分组
        $group_sql = Db::table($order_sql . ' a')->order('id desc')->group('type,course_id')->buildSql();

        $list = Db::table($group_sql . ' b')->where($where)->paginate()->each(function ($v) {
            switch ($v['type']) {
            case 1:
                $type_desc = '首页';
                break;
            case 2:
                $type_desc = '新闻';
                break;
            case 3:
                $type_desc = '视频';
                break;
            case 4:
                $type_desc = '音频';
                break;
            }
            $v['type_desc'] = $type_desc;
    
            $err_count_sum = Db::name('crontab_log')->where(['type' => $v['type'], 'course_id' => $v['course_id'], 'date' => date('Y-m-d')])->max('err_count');
            $v['err_count'] = $err_count_sum;
            if ($v['type'] == 3 || $v['type'] == 4) {
                $info = Db::name('live')->field('name title,url')->where(['id' => $v['course_id']])->find();
                $type = $v['type'] == 3 ? 1 : 2;
                $do_id = Db::name('check_live_log')->where(['live_id' => $v['course_id'], 'type' => $type, 'status' => 0])->max('id');
                $do_url = url('/admin/index/liveDetail', ['id' => $do_id]);
                
                $course_name = $info['title'];
                $url = $info['url'];
            } elseif ($v['type'] == 2) {
                $info = Db::name('news')->field('title,url')->where(['id' => $v['course_id']])->find();
                $do_id = Db::name('check_news_log')->where(['news_id' => $v['course_id'], 'status' => 0])->max('id');
                $do_url = url('/admin/index/newsDetail', ['id' => $do_id]);
                
                $course_name = $info['title'];
                $url = $info['url'];
            } else {
                $do_id = Db::name('check_index_log')->where(['status' => 0])->max('id');
                $do_url = url('/admin/index/indexDetail', ['id' => $do_id]);
                
                $course_name = '';
                $url = 'https://www.xntv.tv';
            }
            
            $v['course_name'] = $course_name;
            $v['url'] = $url;
            $v['do_url'] = $do_url;// 处理链接
            
            return $v;
        });
        
        $this->assign('list', $list);
        $this->assign('title', '预警列表');
        
        return $this->fetch();
    }
    
    public function sirenHistoryList()
    {
        $input = Request()->param();
        
        $type = $input['type'];
        $course_id = $input['course_id'];
        
        $list = Db::name('crontab_log')->where(['type' => $type, 'course_id' => $course_id])->order('id desc')->paginate()->each(function ($v) {
            if ($v['type'] == 3 || $v['type'] == 4) {
                $info = Db::name('live')->field('name title,url')->where(['id' => $v['course_id']])->find();
                
                $course_name = $info['title'];
                $url = $info['url'];
            } elseif ($v['type'] == 2) {
                $info = Db::name('news')->field('title,url')->where(['id' => $v['course_id']])->find();
                
                $course_name = $info['title'];
                $url = $info['url'];
            } else {
                $course_name = '';
                $url = 'https://www.xntv.tv';
            }
            
            $v['course_name'] = $course_name;
            $v['url'] = $url;
            
            return $v;
        });
        
        $this->assign('list', $list);
        $this->assign('title', '预警历史记录');
        
        return $this->fetch();
    }
}
